// 店面管理的控制器
food.controller('ShopListConroller',function($http,$scope,page){


	
   // 查询总条数
   $http({
    
    method:'get',
    url:'../Api/admin.php?action=shop_count'

   }).then(function(res){
    
    $scope.totalItems=res.data.c;

    console.log($scope.totalItems);

   });

   // 分页
   
 $scope.currentPage=1;
 $scope.bigTotalItems=8;
 $scope.maxSize=5;
 var url="../Api/admin.php?action=shop_list";
 page.getData(url,$scope.currentPage,$scope.bigTotalItems).then(function(res){
   $scope.shop_list=res;
 });

$scope.pageChanged=function(){
    page.getData(url,$scope.currentPage,$scope.bigTotalItems).then(function(res){
   $scope.shop_list=res;
 });

};



});

food.controller('ShopAddConroller',function($http,$scope,$location,Upload){

 // 查询所属地区
	
	$http({
		method:'get',
		url:'../Api/admin.php?action=region_cate'
	}).then(function(res){
        $scope.region_list=res.data;
        console.log($scope.region_list);
	});

	// 提交图片文件的方法
    $scope.uploadImg = function(){

        if($scope.shop.shop_img){
            console.log($scope.shop.shop_img);
            Upload.upload({
                url:"../Api/admin.php?action=shop_add",

                file:$scope.shop.shop_img

            }).success(function(result){
                $scope.shop.shop_img = "uploads/"+result.shop_img;
            });
        }
    };

	// 提交表单的方法、
	
    $scope.saveForm = function(){

        $http({
            method:"POST",
            url:"../Api/admin.php?action=shop_add",
            data:$scope.shop,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            transformRequest:function(obj){
                var str = [];
                for(var s in obj){

                    str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));
                }

                return str.join("&");
            }


        }).then(function(result){
            if(result.data){
                $location.path('shop_list');
            }else{
                alert('添加失败');
                $location.path('shop_list');
            }
        });
    }


});

food.controller('ShopEditeConroller',function($scope,$http,$location,$stateParams,Upload,$sce){

	$http({
		method:'get',
		url:'../Api/admin.php?action=region_cate'
	}).then(function(res){
        $scope.region_list=res.data;
        console.log($scope.region_list);
	});

 // 显示旧信息

  $http({
   method:'get',
   url:'../Api/admin.php?action=shop_detile&shop_id='+$stateParams.shop_id
  }).then(function(res){

  $scope.shop=res.data;
  
 
  });





// 重新修改需要提交的内容

$scope.uploadImg = function(){
        if($scope.shop.shop_img){
            // 返回文件名称
            console.log($scope.shop.shop_img);
            // 进度条插件
            Upload.upload({
                url:"../Api/admin.php?action=shop_edit",//返回图片路径 {'img':'404.jpg'}
                file:$scope.shop.shop_img
            }).success(function(result){
                $scope.shop.shop_img = "uploads/"+result.shop_img;//存到本地文件夹
            });
        }
    };




 $scope.saveForm = function(){
        $http({
            method:"POST",
            url:"../Api/admin.php?action=shop_edit&shop_id="+$stateParams.shop_id,
            data:$scope.shop,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            transformRequest:function(obj){
                var str = [];
                for(var s in obj)
                {
                    str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));
                }

                return str.join("&");
            }
        }).then(function(result){
            if(result.data)
            {
                $location.path('shop_list');
            }else{
                alert('添加失败');
                $location.path('shop_list');
            }
        });
    }

});

food.controller('ShopDeleteConroller',function($http,$scope,$location,$stateParams){

  $http({
  	method:'get',
  	url:'../Api/admin.php?action=shop_delete&shop_id='+$stateParams.shop_id
  }).then(function(res){

        if(res.data){
            $location.path('shop_list');

        }else{
            alert('删除失败');
            $location.path('shop_list');
        }

  });

});

food.controller('RegionListConroller',function($http,$location,$scope,page){

 $http({
   method:'get',
   url:'../Api/admin.php?action=region_count'

 }).then(function(res){
 	 $scope.totalItems=res.data.c; 

 	
 });

 // 组装分页

 $scope.currentPage=1;
 $scope.bigTotalItems=8;
 $scope.maxSize=5;
 var url="../Api/admin.php?action=region_list";
 page.getData(url,$scope.currentPage,$scope.bigTotalItems).then(function(res){
   $scope.region_list=res;
 });

$scope.pageChanged=function(){
    page.getData(url,$scope.currentPage,$scope.bigTotalItems).then(function(res){
   $scope.region_list=res;
 });

};
});



food.controller('RegionAddConroller',function($http,$scope,$location){

$scope.saveForm = function(){

        $http({
            method:"POST",
            url:"../Api/admin.php?action=region_add",
            data:$scope.region,

            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            transformRequest:function(obj){
                var str = [];
                for(var s in obj){
                    str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));
                }
                return str.join("&");
            }
        }).then(function(result){

            if(result.data){
                $location.path('region_list');
            }else{
                alert('添加失败');
                $location.path('region_list');
            }
        });
    }


});

food.controller('RegionEditeConroller',function($http,$scope,$location,$stateParams){
   
   // 查询旧信息
   
   $http({
    
     method:'get',
     url:'../Api/admin.php?action=region_detail&region_id='+$stateParams.region_id
   }).then(function(res){

   $scope.region=res.data;
   });
 // 提交修改后的信息
  
$scope.saveForm = function(){

        $http({
            method:"POST",
            url:'../Api/admin.php?action=region_edit&region_id='+$stateParams.region_id,
            // 提交的数据
            data:$scope.region,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            transformRequest:function(obj){
                var str = [];
                for(var s in obj){

                    str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));
                }

                return str.join("&");
            }
        }).then(function(result){
            if(result.data){
                $location.path('region_list');
            }else{
                alert('修改失败');
                $location.path('region_list');
            }
        });
    }

});

food.controller('RegionDeleteConroller',function($http,$scope,$location,$stateParams){


  $http({
     
     url:'../Api/admin.php?action=region_delete&region_id='+$stateParams.region_id,
     method:'get'

  }).then(function(res){
      if(res.data){
         $location.path("region_list");
      }else{
      	alert("删除失败");
      	$location.path("region_list");
      }

  });

})
